/*
 * Created on 2007-2-18
 *
 * TODO
 */
package jacky.lanlan.song.extension.struts.action;

import static org.mockito.Mockito.*;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.junit.Test;
import org.mockito.MockitoAnnotations.Mock;

/**
 * @author Jacky.Song
 *
 * 完整的远程调用过程测试。
 */
public class RemoteTest_FullStack extends BaseStrutsActionTest {

	@Mock private HttpServletResponse response;
	private StringBuilder realResp= new StringBuilder();
	
	@Override
	protected void onSetUp() {
		ServletOutputStream mockOS=new ServletOutputStream () {
			@Override
			public void write(int b) throws IOException {
				realResp.append((char)b);
			}
		};
		
		try {
			stub(response.getOutputStream()).toReturn(mockOS);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
		
		setResponseWrapper(new HttpServletResponseWrapper(response));
	}
	
	@Test
	public void testGetJavaAnimalsList() throws Exception {
		this.testAndCheck("/POJOTest!get_java_animals_list",
				"[\"Kestrel\",\"Merlin\",\"Tiger\",\"Mustang\",\"Dolphin\"]");
	}

	@Test
	public void testGetJavaAnimalsArray() throws Exception {
		this.testAndCheck("/POJOTest!get_java_animals_array",
				"[\"Dolphin\",\"Tiger\",\"Mustang\"]");
	}
	
	@Test
	public void testGetJavaVersionMap() throws Exception {
		this.testAndCheck("/POJOTest!get_java_version_map",
				"{\"Java 6.0\":\"Mustang\",\"Java 7.0\":\"Dolphin\",\"Java 1.4.2\":\"Mantis\",\"Java 5.0\":\"Tiger\"}");
	}
	
	@Test
	public void testGetTestDomain() throws Exception {
		this.testAndCheck("/POJOTest!get_test_domain",
				"{\"addr\":\"Street 1\",\"carNo\":\"12345\"," +
				"\"emitTime\":{\"date\":1,\"day\":4,\"hours\":8,\"minutes\":0,\"month\":0,\"seconds\":2,\"time\":2008,\"timezoneOffset\":-480,\"year\":70}," +
				"\"installNum\":7,\"justGetter\":false}");
	}
	
	private void testAndCheck(String requestPath,String correctJSONString) {
		setRequestPathInfo(requestPath);
		
		actionPerform();
		
		verifyNoActionErrors();
		verifyInputForward();
		
		assertEquals("应该得到返回值的JSON字符串",correctJSONString,realResp.toString());
	}
	
	@Override
	protected void onTearDown() {
		realResp.delete(0, realResp.length());
	}
}
